package Demo23;

import Demo09.TableDemo;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Test  extends JFrame {
    private  JPanel mainPanel;
    private  JScrollPane scrollPane;
    private  JTable table;
    private DefaultTableModel model;

    private static Connection dbConn = null;

    //构造方法
    public void TableDemo(){
        mainPanel=new JPanel(new BorderLayout());//边界布局
//        model=new DefaultTableModel()
        //表头
        String[] heads={"学号","姓名","性别","团员否","籍贯","出生日期","家庭住址","联系方式","入学日期"};

        model=new DefaultTableModel(heads,10);

        table=new JTable(model);

        scrollPane=new JScrollPane(table);


        setContentPane(mainPanel);
        mainPanel.add(scrollPane,"Center");


        setBounds(100,100,500,400);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        fillData();

    }

    private void fillData() {

    }

    public static void main(String[] args) {
        //创建一个连接
//        String dbURL = "jdbc:sqlserver://192.168.1.125:1433;DatabaseName=sa";
        String dbURL="jdbc:sqlserver://localhost:1433;databaseName=SMIS";

        try {
            //1.加载驱动
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

            System.out.println("加载驱动成功！");
            //2.连接
            dbConn = DriverManager.getConnection(dbURL, "sa", "x");

            System.out.println("连接数据库成功！");
            System.out.print("stuId\t");
            System.out.print("stuName\t");
            System.out.print("stuSex\t");
            System.out.print("stuBirthday\t");
            System.out.print("stuAddress\t");
            System.out.print("stuPhone\t");
            System.out.print("clsId\n");
//            System.out.println("\n");
            String sql="select * from student";

            PreparedStatement statement=null;
            statement=dbConn.prepareStatement(sql);
            ResultSet res=null;
            res=statement.executeQuery();
            while(res.next()){
                String title=res.getString("stuId");
                String title1=res.getString("stuName");
                String title2=res.getString("stuSex");
                String title3=res.getString("stuBirthday");
                String title4=res.getString("stuAddress");
                String title5=res.getString("stuPhone");
                String title6=res.getString("clsId");
                System.out.print(title+'\t');
                System.out.print(title1+'\t');
                System.out.print(title2+'\t');
                System.out.print(title3+'\t');
                System.out.print(title4+'\t');
                System.out.print(title5+'\t');
                System.out.print(title6+'\t');
                System.out.println("\n");
            }
        }catch(Exception e) {
            e.printStackTrace();
            System.out.println("连接数据库失败！");
        }

//        new TableDemo();
    }
}

